package com.sunplus.system.registration.controller.api;

import com.sunplus.common.core.controller.BaseController;
import com.sunplus.common.core.domain.AjaxResult;
import com.sunplus.system.registration.domain.PortalRecord;
import com.sunplus.system.registration.service.IPortalRecordService;
import com.sunplus.system.registration.utils.MouldCodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字段信息Controller
 *
 * @author sunplus
 * @date 2021-03-25
 */
@RestController
@RequestMapping("/app/")
public class ApiPortalRecordController extends BaseController {

    @Autowired
    private IPortalRecordService portalRecordService;

    /**
     * 新增记录
     */
    @PostMapping("record/marriage/add")
    public AjaxResult recordMarriageAdd(PortalRecord portalRecord) {
        portalRecord.setRecord_type(1);
        String identityCode = portalRecord.getOwn_identity_code() + portalRecord.getPartner_identity_code();
        String mouldCode = MouldCodeUtil.getMarriageCode(identityCode);
        portalRecord.setMould_code(mouldCode);
        return toAjax(portalRecordService.insertPortalRecord(portalRecord));
    }

    @PostMapping("record/adoption/add")
    public AjaxResult recordAdoptionAdd(PortalRecord portalRecord) {
        portalRecord.setRecord_type(2);
        String identityCode = portalRecord.getOwn_identity_code();
        String mouldCode = MouldCodeUtil.getAdoptionCode(identityCode);
        portalRecord.setMould_code(mouldCode);
        return toAjax(portalRecordService.insertPortalRecord(portalRecord));
    }

    @PostMapping("record/marriage/list")
    public AjaxResult recordMarriageList(PortalRecord portalRecord) {
        if (StringUtils.isNotBlank(portalRecord.getUser_pid())) {
            portalRecord.setRecord_type(1);
            List<PortalRecord> list = portalRecordService.selectPortalRecordList(portalRecord);
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error("获取失败，没有用户id");
        }
    }

    @PostMapping("record/adoption/list")
    public AjaxResult recordAdoptionList(PortalRecord portalRecord) {
        if (StringUtils.isNotBlank(portalRecord.getUser_pid())) {
            portalRecord.setRecord_type(2);
            List<PortalRecord> list = portalRecordService.selectPortalRecordList(portalRecord);
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error("获取失败，没有用户id");
        }
    }

    /**
     * 获取记录详细信息
     */
    @PostMapping("record/info")
    public AjaxResult recordMarriageInfo( String pid) {
        return AjaxResult.success(portalRecordService.selectPortalRecordById(pid));
    }


}
